package com.ztjava.tieba.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Page;
import com.xiaoleilu.hutool.date.DateUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.member.MobileClient;
import com.ztjava.core.tool.ViewTool;
import com.ztjava.core.utiliy.StringUtil;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.tieba.model.TiebaCatalog;
import com.ztjava.tieba.model.TiebaContent;
import com.ztjava.tieba.model.TiebaReply;
import com.ztjava.tieba.model.TiebaSubject;
import com.ztjava.tieba.service.TiebaService;
import com.ztjava.tieba.validator.AddContentValidator;
import com.ztjava.tieba.validator.AddReplyValidator;
@ControllerClass(client=MobileClient.class,path="/tieba")
@UserController(portal=Constant.P_MEMBER)
public class TiebaMobileController extends BaseController {
	private static TiebaService tiebaService = Duang.duang(TiebaService.class);
	public void list(){
		Integer id = getParaToInt("id");
		TiebaSubject tiebaSubject = null;
		if(id!=null&&id>0){
			tiebaSubject = TiebaSubject.dao.findById(id);
		}else {
			String code = getPara("code");
			if(code!=null&&!code.equals("")){
				tiebaSubject = tiebaService.getSubjectByCode(code);
			}
		}
		if(tiebaSubject==null||tiebaSubject.getStr("enable_flag").equals("F")){
			renderText("主题不存在！");
			return;
		}
		tiebaSubject.set("click", tiebaSubject.getInt("click")+1).update();
		setAttr("tiebaSubject", tiebaSubject);
		render(tiebaSubject.getStr("list_tpl"));
	}
	
	public void ajaxLoadContent(){
		Page<TiebaContent> pd = tiebaService.getContentPage(client, getParaMapCustom());
		for(int i=0;i<pd.getList().size();i++){
			pd.getList().get(i).put("create_time_str", ViewTool.dateChange(pd.getList().get(i).getTimestamp("create_time")));
		}
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr("infoList",pd.getList());
		renderJson(new String[]{Constant.RESULT,"infoList"});
	}
	
	public void show(){
		Integer id = getParaToInt("id");
		TiebaContent tiebaContent = TiebaContent.dao.findById(id);
		if(tiebaContent==null||tiebaContent.getStr("enable_flag").equals("F")){
			renderText("内容不存在！");
			return;
		}
		TiebaSubject tiebaSubject = TiebaSubject.dao.findById(tiebaContent.getInt("subject_id"));
		if(tiebaSubject==null||tiebaSubject.getStr("enable_flag").equals("F")){
			renderText("主题不存在！");
			return;
		}
		setAttr("tiebaSubject", tiebaSubject);
		tiebaContent.set("click", tiebaContent.getInt("click")+1).update();
		setAttr("tiebaContent", tiebaContent);
		render(tiebaSubject.getStr("show_tpl"));
	}
	
	@UserMethod
	public void addContent(){
		keepPara("subject_id");
		List<TiebaCatalog> catalogList = tiebaService.getCatalogList(getParaToInt("subject_id"));
		setAttr("catalogList", catalogList);
		render("addContent.html");
	}
	
	@UserMethod
	@Before(AddContentValidator.class)
	public void doAddContent(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TiebaContent tiebaContent = new TiebaContent();
		tiebaContent.set("subject_id", getParaToInt("subject_id"));
		tiebaContent.set("catalog_id", getParaToInt("catalog_id"));
		tiebaContent.set("name", getPara("name"));
		tiebaContent.set("content", getPara("content"));
		tiebaContent.set("images", StringUtil.multiple2(getParaValues("image")));
		tiebaContent.set("last_reply_time", DateUtil.now());
		tiebaContent.set("is_pass", ViewTool.param("TIEBA_CONTENT_PASS"));
		tiebaContent.set("create_user", authSession.getId());
		tiebaContent.save();
		renderSuccess(new String[]{"id",tiebaContent.getInt("id").toString()});
	}
	
	public void ajaxLoadReply(){
		Page<TiebaReply> pd = tiebaService.getReplyPage(client, 
				getParaMapCustom(new String[]{"parent_id","0"},
				new String[]{"enable_flag","T"}));
		for(int i=0;i<pd.getList().size();i++){
			pd.getList().get(i).put("create_time_str", ViewTool.dateChange(pd.getList().get(i).getTimestamp("create_time")));
		}
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr("infoList",pd.getList());
		renderJson(new String[]{Constant.RESULT,"infoList"});
	}
	
	@UserMethod
	public void addReply(){
		
		render("addReply.html");
	}
	
	@UserMethod
	@Before(AddReplyValidator.class)
	public void doAddReply(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Integer content_id = getParaToInt("content_id");
		TiebaReply tiebaReply = new TiebaReply();
		tiebaReply.set("content_id", content_id);
		tiebaReply.set("parent_id", getParaToInt("parent_id",0));
		tiebaReply.set("reply_user_id", getParaToInt("reply_user_id",0));
		tiebaReply.set("reply", getPara("reply"));
		tiebaReply.set("is_pass", ViewTool.param("TIEBA_REPLY_PASS"));
		tiebaReply.set("create_user", authSession.getId());
		tiebaReply.save();
		tiebaService.updateContentReply(content_id);
		renderSuccess();
	}
}
